package com.flyqiu.form.store.mysql;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.flyqiu.common.tools.FlyQiuFileTools;
import com.flyqiu.form.api.FlyQiuFormContext;
import com.flyqiu.form.api.spi.FlyQiuFormStoreInitialization;
import com.flyqiu.form.api.store.FlyQiuFormStoreService;
import com.flyqiu.form.common.col.SqlColType;
import com.flyqiu.form.model.DefaultSqlColType;
import com.flyqiu.form.store.mysql.mapper.FlyQiuFormMySqlStoreMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * MySQL存储初始化实现类。
 */
@Configuration
@MapperScan(basePackageClasses = {FlyQiuFormMySqlStoreMapper.class})
public class FlyQiuMySqlFormStoreInitialization implements FlyQiuFormStoreInitialization {

    private final Map<String, SqlColType> sqlColumnTypeMap = new HashMap<>();

    /**
     * 构造函数，初始化SQL列类型映射。
     */
    public FlyQiuMySqlFormStoreInitialization() {
        initSqlType();
    }

    /**
     * 初始化SQL列类型映射。
     */
    private void initSqlType() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        InputStream resourceAsStream = classLoader.getResourceAsStream("flyQiuFormColumn/mySqlDefaultColumnType.json");
        assert resourceAsStream != null;
        try {
            String content = FlyQiuFileTools.readInputStream(resourceAsStream, StandardCharsets.UTF_8);
            JSONObject jsonObject = JSONObject.parseObject(content);
            Set<String> keySet = jsonObject.keySet();
            for (String key : keySet) {
                JSONObject colType = jsonObject.getJSONObject(key);
                if (colType == null || colType.isEmpty()) {
                    continue;
                }
                DefaultSqlColType sqlColType = colType.toJavaObject(DefaultSqlColType.class);
                sqlColumnTypeMap.put(key, sqlColType);
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to initialize SQL column type map", e);
        }
    }

    /**
     * 获取Mapper包路径。
     */
    private static final String MAPPER_PACKAGE = FlyQiuFormMySqlStoreMapper.class.getPackageName();

    /**
     * 获取存储标识。
     *
     * @return 存储标识
     */
    @Override
    public String getStorageKey() {
        return FlyQiuFormMySqlStore.STORAGE_KEY;
    }

    /**
     * 获取SQL列类型配置。
     *
     * @return SQL列类型映射
     */
    @Override
    public Map<String, SqlColType> getColTypeConfig() {
        return sqlColumnTypeMap;
    }

    /**
     * 初始化Mapper。
     *
     * @param configuration Mybatis配置对象
     */
    @Override
    public void initMapper(MybatisConfiguration configuration) {
        configuration.addMappers(MAPPER_PACKAGE);
    }

    /**
     * 初始化表单存储服务。
     *
     * @param context 表单上下文
     * @return 表单存储服务实例
     */
    @Override
    public FlyQiuFormStoreService initFormStore(FlyQiuFormContext context) {
        return new FlyQiuFormMySqlStore(context);
    }
}
